layui.define(function(exports){
	var $= layui.jquery;	
	obj = {
		settings:{},
		data:{},
		value : null,
		loops : null,
		increment : null,
		loopCount : 0,
		init : function (data) {
			obj.settings.id             = data.id             ? data.id             : '';
			obj.settings.from           = data.from           ? data.from           : 0;
			obj.settings.to             = data.to             ? data.to             : 0;
			obj.settings.speed          = data.speed          ? data.speed          : 1000 //动画完成总耗时间
			obj.settings.refreshInterval= data.refreshInterval? data.refreshInterval: 100; //动画刷新时间间隔
			obj.settings.decimals       = data.decimals       ? data.decimals       : 2;
			obj.settings.onUpdate       = data.onUpdate       ? data.onUpdate       : obj.onUpdate;
			obj.settings.onComplete     = data.onComplete     ? data.onComplete     : obj.onComplete;
			obj.loops = Math.ceil(obj.settings.speed / obj.settings.refreshInterval);//计算动画秒数
		    obj.increment = (obj.settings.to - obj.settings.from) / obj.loops, //计算步幅
			obj.value = obj.settings.from;			
			obj.interval = setInterval(obj.updateTimer, obj.settings.refreshInterval);
			obj.render(obj.value);
		},
		updateTimer : function(){
			obj.value += obj.increment;
			obj.loopCount++;
			obj.render(obj.value);
			if (typeof(obj.settings.onUpdate) == 'function') {
				obj.settings.onUpdate(obj.value);
			}
			if (obj.loopCount >= obj.loops) {
				clearInterval(obj.data.interval);
				obj.value = obj.settings.to;
				obj.render(obj.value);
				if (typeof(obj.settings.onComplete) == 'function') {
					obj.settings.onComplete(obj.value);
				}
			}
		},
		render : function (value){
			var formattedValue = obj.formatter(value);
			$('#'+obj.settings.id).html(formattedValue);	
		},
		formatter : function (value) {
			return value.toFixed(obj.settings.decimals).replace(/\B(?=(?:\d{3})+(?!\d))/g, ',');
		},
		onUpdate : function(value){},
		onComplete : function(value){}
		
	}
 	exports('numScroll', obj);
});